﻿namespace HuaWei.Windows.Forms
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;

    public class AsmReflect
    {
        public static object GetClassAssembly(string asmString)
        {
            if (string.IsNullOrEmpty(asmString))
            {
                return null;
            }
            object obj3 = null;
            string[] strArray = asmString.Split(new char[] { '|' });
            string str = strArray[0];
            string typeName = strArray[1];
            try
            {
                System.Type type;
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, str + ".dll");
                if (str.Trim().Length == 0)
                {
                    type = System.Type.GetType(typeName);
                    if (type == null)
                    {
                        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                        int index = 0;
                        while (true)
                        {
                            if (index >= assemblies.Length)
                            {
                                break;
                            }
                            try
                            {
                                string fullName = assemblies[index].FullName;
                                string str5 = typeName.Substring(0, typeName.LastIndexOf('.'));
                                if (fullName.Contains(str5))
                                {
                                    type = assemblies[index].GetType(typeName);
                                    if (type != null)
                                    {
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                            }
                            index++;
                        }
                    }
                    if (type != null)
                    {
                        obj3 = Activator.CreateInstance(type);
                    }
                    return obj3;
                }
                if (File.Exists(path))
                {
                    type = Assembly.LoadFrom(path).GetType(typeName);
                    if (type != null)
                    {
                        obj3 = Activator.CreateInstance(type);
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return obj3;
        }

        public static object GetClassAssembly(string asmString, string interfaceName)
        {
            if (string.IsNullOrEmpty(asmString))
            {
                return null;
            }
            object obj3 = null;
            string[] strArray = asmString.Split(new char[] { '|' });
            string str = strArray[0];
            string typeName = strArray[1];
            try
            {
                System.Type type;
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, str + ".dll");
                if (str.Trim().Length == 0)
                {
                    type = System.Type.GetType(typeName);
                    if (type == null)
                    {
                        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                        int index = 0;
                        while (true)
                        {
                            if (index >= assemblies.Length)
                            {
                                break;
                            }
                            try
                            {
                                string fullName = assemblies[index].FullName;
                                string str5 = typeName.Substring(0, typeName.LastIndexOf('.'));
                                if (fullName.Contains(str5))
                                {
                                    type = assemblies[index].GetType(typeName);
                                    if (type != null)
                                    {
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                            }
                            index++;
                        }
                    }
                    if ((type != null) && (type.GetInterface(interfaceName) != null))
                    {
                        obj3 = Activator.CreateInstance(type);
                    }
                    return obj3;
                }
                if (File.Exists(path))
                {
                    type = Assembly.LoadFrom(path).GetType(typeName);
                    if ((type != null) && (type.GetInterface(interfaceName) != null))
                    {
                        obj3 = Activator.CreateInstance(type);
                    }
                }
                else if (!File.Exists(path))
                {
                    string str6 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[] { '\\' }).Replace("Framework", ""), str + ".dll");
                    if (File.Exists(str6))
                    {
                        type = Assembly.LoadFrom(str6).GetType(typeName);
                        if ((type != null) && (type.GetInterface(interfaceName) != null))
                        {
                            obj3 = Activator.CreateInstance(type);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return obj3;
        }
    }
}

